import Clipboard from 'clipboard';
export function pageto(str, params) {
	if (!!params) {
		uni.switchTab({
			url: str
		})
	} else {
		uni.navigateTo({
			url: str
		})
	}
}
export function pageback() {
	uni.navigateBack({
		delta: 1
	})
}
export function isToken(token, bool = true) {
	if (!token) {
		setTimeout(() => {
			uni.navigateTo({
				url: '/pages/index/index'
			})
		}, 2000)
	}
}
export function _updataNavTitle(text) {
	uni.setNavigationBarTitle({
		title: text
	})
}
export function _updataTabBar(arr, lang) {
	var data = arr[lang];
	for (var index in data) {
		uni.setTabBarItem({
			index: +index,
			text: data[+index]
		})
	}
}
export function checkLogin(logininfo, url) {
	if (!logininfo.id) {
		redirectologin()
		return Promise.resolve(false)
	}
	let _data = {
		id: logininfo.id,
		token: logininfo.token
	}
	return new Promise((resolve, reject) => {
		fetch(url + '/api/index/index2', _data, "post")
			.then(res => {
				if (res.statusCode == 200 && res.data.code == 1) {
					resolve(true)
				} else {
					redirectologin()
				}
			})
			.catch(err => {
				redirectologin()
			})
	})
}
export function redirectologin() {
	uni.redirectTo({
		url: "/pages/index/index"
	})
}
export function diff_txt(time, lang) {
	let chs = {
		lang1: '今天',
		lang2: '昨天',
		lang3: '前天',
	}
	let en = {
		lang1: 'Today ',
		lang2: 'Yesterday ',
		lang3: 'The day before yesterday ',
	}
	let _lang = lang == "chs" ? chs : en
	if (!time) return {};
	let time1 = (new Date(time)).getDate();
	let time2 = (new Date()).getDate();
	if ((new Date()).getFullYear() == (new Date(time)).getFullYear() && (new Date()).getMonth() == (new Date(time))
		.getMonth()) {
		if (time1 == time2) {
			return _lang.lang1 + time.slice(11);
		} else if (time2 - time1 == 1) {
			return _lang.lang2 + time.slice(11);
		} else if (time2 - time1 == 2) {
			return _lang.lang3 + time.slice(11);
		} else {
			return time;
		}
	}
	return time;
}
export function showToast(title = "", duration = 2000, mask = true, icon = "none") {
	if (!title) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
export const fetch = async (url, data = {}, method = "GET", header) => {
	const [error, res] = await uni.request({
		url,
		data,
		method,
		header
	})
	if (error) {
		return error;
	} else {
		return res;
	}
}
export function forceUpdate () {
	// #ifdef APP-PLUS
	var server = "http://yftest.sqyk.net/api/index/app_info"; //检查更新地
	var req = { //升级检测数据  
		"appid": plus.runtime.appid,
		"version": plus.runtime.version
	};
	let _this = this;
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		const {
			name,
			version
		} = widgetInfo
		uni.request({
			url: server,
			data: '',
			success: (res) => {
				if (res.data.code == 1) {
					//enforce  1更新，0不更新
					if (res.data.data.enforce == 1) {
						if (uni.getSystemInfoSync().platform == 'ios') {
							//判断是否ios更新
							if (req.version == res.data.data.oldversion) {
								// ios版本号相同不提示更新
							} else {
								// ios版本号不同提示更新
							}
						} else if (uni.getSystemInfoSync().platform === 'android') {
							//判断是否安卓更新
							_this.versionnew = res.data.data.oldversion
							if (req.version == res.data.data.oldversion) {} else {

								if (!!res.data.data.downloadurl_apple) {

									uni.downloadFile({
										url: res.data.data.downloadurl_apple,
										success: (res) => {
											console.log(res);
											if (res.statusCode === 200) {
												plus.runtime.install(res
													.tempFilePath, { //进行自动安装
														force: false
													},
													function() {
														console.log(
															'install success...'
														);
														// plus.runtime.restart(); //安装成功后重启应用
														uni.showToast({
															title: '已更新至最新版本,请重启程序',
															icon: 'none'
														})
													},
													function(e) {
														console.error(
															'install fail...'
														);
													});
											}

										}
									})
								}
							}
						}
					}
				}
			}
		})
	})
	// #endif
}
export const handleClipboard = (text, event, onSuccess, onError) => {
	event = event || {}
	const clipboard = new Clipboard(event.target, {
		text: () => text
	})
	clipboard.on('success', () => {
		onSuccess()
		clipboard.off('error')
		clipboard.off('success')
		clipboard.destroy()
	})
	clipboard.on('error', () => {
		onError()
		clipboard.off('error')
		clipboard.off('success')
		clipboard.destroy()
	})
	clipboard.onClick(event)
}
